<?php
    /**
     * Feature a group
     *
     * @package ElggGroups
     */

    $group_guid = get_input('group_guid');
    $action = get_input('action_type');

    $group = get_entity($group_guid);

    if (!elgg_instanceof($group, 'group')) {
        register_error(elgg_echo('groups:featured_error'));
        forward(REFERER);
    }

//get the action, is it to feature or unfeature
    if ($action == "feature") {
        $group->featured_group = "yes";
        system_message(elgg_echo('groups:featuredon', array($group->name)));
    } else {
        // check if user has disliked item
        if (elgg_annotation_exists($group_guid, 'dislikes')) {
            $dislikes = elgg_get_annotations(array(
                'guid' => (int)get_input('group_guid'),
                'annotation_owner_guid' => elgg_get_logged_in_user_guid(),
                'annotation_name' => 'dislikes',
            ));
            if ($dislikes) {
                if ($dislikes[0]->canEdit()) {
                    $dislikes[0]->delete();
                    system_message(elgg_echo("dislikes:deleted"));
                }
            }
        }

        $group->featured_group = "no";
        system_message(elgg_echo('groups:unfeatured', array($group->name)));
    }

    forward(REFERER);
